<template>
  <div class="app-container">
    <!--查询参数-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="表名称" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--表格渲染-->
    <el-table ref="table" v-loading="loading" :data="tableList" style="width: 100%;"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="tableName" label="表名"/>
      <el-table-column :show-overflow-tooltip="true" prop="engine" label="数据库引擎"/>
      <el-table-column :show-overflow-tooltip="true" prop="coding" label="字符编码集"/>
      <el-table-column :show-overflow-tooltip="true" prop="remark" label="备注"/>
      <el-table-column prop="createTime" label="创建日期">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="270px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
            v-hasRoles="['tool:generator:preview']"
          >预览
          </el-button>
          <el-button size="mini" icon="el-icon-download" type="text" @click="toDownload(scope.row.tableName)">下载
          </el-button>

          <el-button
            type="text"
            size="small"
            icon="el-icon-setting"
            @click="handleEditTable(scope.row)"
            v-hasRoles="['tool:generator:edit']"
          >
            配置
          </el-button>

          <el-button type="text" icon="el-icon-printer" size="mini" @click="toGen(scope.row.tableName)">生成</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  // import CRUD, { presenter, header } from '@crud/crud'
  import {downloadFile} from '@/utils/index'
  import {getTableList, generator} from "@/api/generator/generator";

  export default {
    name: "index",
    // mixins: [presenter(), header()],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 唯一标识符
        uniqueId: "",
        // 选中数组
        ids: [],
        // 选中表数组
        tableNames: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 表数据
        tableList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          tableName: undefined
        },
      }
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询表集合 */
      getList() {
        this.loading = true;
        getTableList(this.queryParams).then(response => {
            const {content, totalElements} = response.data
            this.tableList = content;
            this.total = totalElements;
            this.loading = false;
          }
        );
      },
      toGen(tableName) {
        // 生成代码
        generator(tableName, 0).then(data => {
          this.$notify({
            title: '生成成功',
            type: 'success',
            duration: 2500
          })
        })
      },
      toDownload(tableName) {
        // 打包下载
        generator(tableName, 2).then(data => {
          downloadFile(data.data, tableName, 'zip')
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 预览按钮 */
      handlePreview(row) {
        this.$router.push("/tool/generator/preview/" + row.tableName);
      },
      /**
       * 编辑按钮
       * @param selection
       */
      handleEditTable(row) {

        this.$router.push("/tool/generator/config/" + row.tableName);
      },

      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.tableId);
        this.tableNames = selection.map(item => item.tableName);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
    }
  }
</script>

<style scoped>

</style>
